<template>
    <div
        v-if="isExternal"
        :style="styleExternalIcon"
        class="svg-external-icon svg-icon"
        v-on="$listeners"
    ></div>
    <span v-else-if="isElicon" :class="svgClass"></span>
    <svg v-else :class="svgClass" aria-hidden="true" v-on="$listeners">
        <use :xlink:href="iconName" />
    </svg>
</template>
<script>
import { isExternal, isElicon } from '@/utils/validate'
export default {
    name: 'SvgIcon',
    props: {
        /**
         * 图标的class名称
         */
        iconClass: {
            type: String,
            required: true
        },
        /**
         * svg的className
         */
        className: {
            type: String,
            default: ''
        }
    },
    computed: {
        /**
         * 判断是否为网络地址
         */
        isExternal() {
            return isExternal(this.iconClass)
        },
        /**
         * 判断是否为element的图标
         */
        isElicon() {
            return isElicon(this.iconClass)
        },
        /**
         * 合成svg的图标class
         */
        iconName() {
            return `#icon-${this.iconClass}`
        },
        svgClass() {
            if (this.className) {
                return 'svg-icon ' + this.className
            } else {
                return 'svg-icon'
            }
        },
        styleExternalIcon() {
            return {
                mask: `url(${this.iconClass}) no-repeat 50% 50%`,
                '-webkit-mask': `url(${this.iconClass}) no-repeat 50% 50%`
            }
        }
    }
}
</script>

<style scoped lang="scss">
.svg-icon {
    overflow: hidden;
    width: 1em;
    height: 1em;
    vertical-align: -0.15em;
    fill: currentcolor;
}
.svg-external-icon {
    display: inline-block;
    background-color: currentcolor;
    mask-size: cover !important;
}
</style>
